﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace OI.Framework
{
    public class WatermarkTextBox : TextBox
    {
        private TextBlock WatermarkTextBlock;
        private string _Watermark;
        private Color _WatermarkForeground;


        public WatermarkTextBox()
        {
            TextChanged += TextChangedHandler;
        }

        public new string Watermark
        {
            get { return _Watermark; }
            set
            {
                _Watermark = value;
                if (WatermarkTextBlock != null)
                {
                    WatermarkTextBlock.Text = _Watermark ?? "";
                    TextChangedHandler(null, null);
                }
            }
        }


        public new string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                TextChangedHandler(null, null);
            }
        }


        /// <summary>
        /// Gets or sets the watermark foreground.
        /// </summary>
        /// <value>The watermark foreground.</value>
        public Color WatermarkForeground
        {
            get { return _WatermarkForeground; }
            set
            {
                _WatermarkForeground = value;
                if (WatermarkTextBlock != null)
                {
                    WatermarkTextBlock.Foreground = new SolidColorBrush(_WatermarkForeground);
                }
            }
        }


        private void TextChangedHandler(object sender, TextChangedEventArgs e)
        {
            WatermarkTextBlock.Visibility = !string.IsNullOrEmpty(Text) ? Visibility.Collapsed : Visibility.Visible;
        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            WatermarkTextBlock = (TextBlock) (GetTemplateChild("_Watermark"));
            WatermarkTextBlock.Text = Watermark ?? "";
            TextChangedHandler(null, null);
        }
    }
}